package com.splant.android.smart.UILevel.Community;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.splant.android.R;
import com.splant.android.smart.CommunicationLevel.sPlantAsyncHttpClient;
import com.splant.android.smart.CommunicationLevel.sPlantJsonHttpResponseHandler;
import com.splant.android.smart.ModelLevel.PlantBean;

import org.apache.http.Header;
import org.json.JSONObject;

public class Garden_Plant_Activity extends Activity implements View.OnClickListener {

    private Button btn_goBack;
    private TextView tv_plantName;
    private TextView tv_soilHumidity;
    private TextView tv_soilTemp;
    private TextView tv_soilNutrient;
    private TextView tv_envTemp;
    private TextView tv_plantDesc;
    private ImageView img_plant;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.garden_plant_activity);

        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        tv_plantName= (TextView) findViewById(R.id.tv_plantName);
        tv_soilHumidity = (TextView) findViewById(R.id.tv_soilHumidity);
        tv_soilTemp = (TextView) findViewById(R.id.tv_soilTemp);
        tv_soilNutrient = (TextView) findViewById(R.id.tv_soilNutrient);
        tv_envTemp = (TextView) findViewById(R.id.tv_envTemp);
        tv_plantDesc = (TextView) findViewById(R.id.tv_plantDesc);
        img_plant = (ImageView) findViewById(R.id.img_plant);

        init();

       btn_goBack.setOnClickListener(this);

    }

    private String plantId = "";
    private sPlantAsyncHttpClient sPlantClient;

    private void init() {

        plantId = getIntent().getStringExtra("plantId");

        sPlantClient = sPlantAsyncHttpClient.getInstance();

        getData();
    }

    public void getData() {

        RequestParams params = new RequestParams();
        params.put("plantId", plantId);

        sPlantClient.get(sPlantAsyncHttpClient.GETPLANT, params, new sPlantJsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                    if (response.getInt("succeed") == 1) {
                        Gson gson = new Gson();

                        PlantBean plantBean = gson.fromJson(response.toString(), PlantBean.class);

                        if (plantBean.getPlant() != null) {

                            tv_plantName.setText(plantBean.getPlant().getPlantName());
                            tv_soilHumidity.setText(plantBean.getPlant().getSoilHumidity());
                            tv_soilTemp.setText(plantBean.getPlant().getSoilTemp());
                            tv_soilNutrient.setText(plantBean.getPlant().getSoilNutrient());
                            tv_envTemp.setText(plantBean.getPlant().getEnvTemp());
                            tv_plantDesc.setText(plantBean.getPlant().getPlantDesc());

                            Glide.with(Garden_Plant_Activity.this)
                                    .load(plantBean.getPlant().getPlantImgUrl())
                                    .dontAnimate()
                                    .placeholder(R.mipmap.garden_ic_plantimg_fail)
                                    .into(img_plant);
                        }

                    }
                } catch (Exception e) {

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {

                Toast.makeText(Garden_Plant_Activity.this, getString(R.string.system_request_error), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_goBack:
                finish();
                break;
        }
    }
}
